Skip to content

Conversation

declanvk
Copy link
Contributor

@declanvk declanvk commented Apr 1, 2022

Description

Add the addr, with_addr, map_addr methods to the NonNull type, and map the address type to NonZeroUsize.

Motivation

The NonNull type is useful for implementing pointer types which have the 0-niche. It is currently possible to implement these provenance preserving functions by calling NonNull::as_ptr and new_unchecked. The adding these methods makes it more ergonomic.

Testing

Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a NonZeroUsize and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance.

Related

Extension of APIs proposed in #95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).

**Description**
 Add the `addr`, `with_addr, `map_addr` methods to the `NonNull` type,
 and map the address type to `NonZeroUsize`.

 **Motiviation**
 The `NonNull` type is useful for implementing pointer types which have
 the 0-niche. It is currently possible to implement these provenance
 preserving functions by calling `NonNull::as_ptr` and `new_unchecked`.
 The addition of these methods simply make it more ergonomic to use.

 **Testing**
 Added a unit test of a nonnull tagged pointer type. This is based on
 some real code I have elsewhere, that currently routes the pointer
 through a `NonZeroUsize` and back out to produce a usable pointer.
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 1, 2022
@dtolnay dtolnay added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-strict-provenance Area: Strict provenance for raw pointers labels Apr 1, 2022
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Apr 1, 2022

@bors r+

@dtolnay
Copy link
Member

dtolnay commented Apr 1, 2022

@bors ping

@bors
Copy link
Collaborator

bors commented Apr 1, 2022

😪 I'm awake I'm awake

@dtolnay
Copy link
Member

dtolnay commented Apr 1, 2022

@bors r+

@bors
Copy link
Collaborator

bors commented Apr 1, 2022

📌 Commit 2a82763 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 1, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 1, 2022
Implement provenance preserving methods on NonNull

### Description
 Add the `addr`, `with_addr`, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`.

 ### Motivation
 The `NonNull` type is useful for implementing pointer types which have  the 0-niche. It is currently possible to implement these provenance  preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The adding these methods makes it more ergonomic.

 ### Testing
 Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance.

### Related

Extension of APIs proposed in rust-lang#95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 1, 2022
Implement provenance preserving methods on NonNull

### Description
 Add the `addr`, `with_addr`, `map_addr` methods to the `NonNull` type, and map the address type to `NonZeroUsize`.

 ### Motivation
 The `NonNull` type is useful for implementing pointer types which have  the 0-niche. It is currently possible to implement these provenance  preserving functions by calling `NonNull::as_ptr` and `new_unchecked`. The adding these methods makes it more ergonomic.

 ### Testing
 Added a unit test of a non-null tagged pointer type. This is based on some real code I have elsewhere, that currently routes the pointer through a `NonZeroUsize` and back out to produce a usable pointer. I wanted to produce an ideal version of the same tagged pointer struct that preserved pointer provenance.

### Related

Extension of APIs proposed in rust-lang#95228 . I can also split this out into a separate tracking issue if that is better (though I may need some pointers on how to do that).
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 2, 2022
Rollup of 8 pull requests

Successful merges:

 - rust-lang#95354 (Handle rustc_const_stable attribute in library feature collector)
 - rust-lang#95373 (invalid_value lint: detect invalid initialization of arrays)
 - rust-lang#95430 (Disable #[thread_local] support on i686-pc-windows-msvc)
 - rust-lang#95544 (Add error message suggestion for missing noreturn in naked function)
 - rust-lang#95556 (Implement provenance preserving methods on NonNull)
 - rust-lang#95557 (Fix `thread_local!` macro to be compatible with `no_implicit_prelude`)
 - rust-lang#95559 (small type system refactoring)
 - rust-lang#95560 (convert more `DefId`s to `LocalDefId`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d6f6084 into rust-lang:master Apr 2, 2022
@rustbot rustbot added this to the 1.61.0 milestone Apr 2, 2022
@declanvk declanvk deleted the nonnull-provenance branch April 2, 2022 07:05

/// Creates a new pointer by mapping `self`'s address to a new one.
///
/// This is a convenience for [`with_addr`][Self::with_addr], see that method for details.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to have a similar reference in the addr and with_addr docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean references to the ptr::addr and ptr::with_addr from the NonNull versions? I can make a followup CR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I mean. That would be great. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-strict-provenance Area: Strict provenance for raw pointers S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants